import logging
import os

from flask import json, jsonify, request
from flask.views import MethodView
from pymemcache import serde
from pymemcache.client.base import Client

MEMCACHED_URL = os.getenv("MEMCACHED_URL", "127.0.0.1:12000")
client = Client(
    MEMCACHED_URL,
    serializer=serde.python_memcache_serializer,
    deserializer=serde.python_memcache_deserializer,
)


class IndexAPI(MethodView):
    def get(self):
        return jsonify({"api_version": "2.0", "message": "Hello World!"})


class SystemIsOpenedAPI(MethodView):
    def get(self):
        if client.get("Status") == "stopped":
            return jsonify({"code": "1", "message": "System is already closed!"})
        return jsonify({"code": "0", "message": "System is already open!"})


class SystemOpenAPI(MethodView):
    def get(self):
        if client.get("Status") == "running":
            return jsonify({"code": "1", "message": "System is already open!"})
        if client.get("Status") == "booting":
            return jsonify({"code": "1", "message": "System is booting!"})
        client.set("Status", "running")
        return jsonify({"code": "0", "message": "Opening System!"})


class SystemCloseAPI(MethodView):
    def get(self):
        if client.get("Status") == "stopped":
            return jsonify({"code": "1", "message": "System is already closed!"})
        if client.get("Status") == "booting":
            return jsonify({"code": "1", "message": "System is booting!"})
        client.set("Status", "stopped")
        detection_info = {"stop_detection": True}
        client.set("detection_info", detection_info)
        client.set("DetectionChanged", True)
        return jsonify({"code": "0", "message": "Closing System!"})


class DecectAPI(MethodView):
    def post(self):
        try:
            request_data = request.get_data()
            request_data = json.loads(request_data)
        except Exception as e:
            logging.error(e)
            return jsonify({"code": "3", "message": "args error!"})
        if client.get("Status") == "booting":
            return jsonify({"code": "5", "message": "System is booting!"})
        if client.get("Status") == "stopped":
            return jsonify({"code": "6", "message": "System is already closed!"})

        if client.get("DetectionChanged"):
            return jsonify({"code": "2", "message": "System is busying!"})

        detection_info = {}
        try:
            detection_info["url"] = request_data["url_1"]
            detection_info["type"] = request_data["type_1"]
            detection_info["stream_name"] = request_data["viName_1"]
            detection_info["time_threshold"] = float(request_data["timeRange_1"])
            detection_info["error_threshold"] = float(request_data["errorThreshold_1"])
        except KeyError as e:
            logging.error(e)
            return jsonify({"code": "3", "message": "args error!"})

        client.set("detection_info", detection_info)

        client.set("DetectionChanged", True)
        return jsonify({"code": "0"})
